package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.NullField;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataBase extends NullField implements ITag {
	public static String DEFAULT_ID = "_Base";

	private GrfxMLEngine engine;

	DataSettings SETTINGS = new DataSettings();
	DataStore _DATA;
	DataStore DATA;
	DataProcess PROCESS;
	DataChildren PROTOTYPES;
	DataMouse MOUSE = new DataMouse();
	DataTitle INFO = new DataTitle();
	DataKey KEY;
	DataNode BACKGROUND;
	DataNode ROOT;
	DataNode GUI;
public DataBase(GrfxMLEngine engine) {

	this.engine = engine;

	PROCESS = new DataProcess(engine);
	PROTOTYPES = new DataChildren(engine,null);
	BACKGROUND = new DataNode(engine,null);
	ROOT = new DataNode(engine,null);
	GUI = new DataNode(engine,null);
	KEY = new DataKey(engine);
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch

	DATA = new DataStore(engine);
	_DATA = new DataStore(engine);

	_DATA.setHashed(true);
}
public void buildFromgrfxML(XmlElement xml) {
	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.BASE_SETTINGS))
		{
			SETTINGS = new DataSettings();
			SETTINGS.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_PROCESS))
		{
			PROCESS = new DataProcess(engine);
			PROCESS.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_DATA))
		{
			DATA = new DataStore(engine);
			DATA.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_PROTOTYPES))
		{
			PROTOTYPES = new DataChildren(engine,null);
			PROTOTYPES.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_MOUSE))
		{
			MOUSE = new DataMouse();
			MOUSE.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_INFO))
		{
			INFO = new DataTitle();
			INFO.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_KEY))
		{
			KEY = new DataKey(engine);
			KEY.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_BACKGROUND))
		{
			BACKGROUND = new DataNode(engine,null);
			BACKGROUND.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_ROOT))
		{
			ROOT = new DataNode(engine,null);
			ROOT.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_GUI))
		{
			GUI = new DataNode(engine,null);
			GUI.buildFromgrfxML(element);
		}
		else if( !NameHelper.isSystemName(element.getName()) )
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.BASE,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml) {

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.BASE_SETTINGS))
		{
			SETTINGS.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_PROCESS))
		{
			PROCESS.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_DATA))
		{
			DATA.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_PROTOTYPES))
		{
			PROTOTYPES.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_MOUSE))
		{
			MOUSE.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_INFO))
		{
			INFO.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_KEY))
		{
			KEY.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_BACKGROUND))
		{
			BACKGROUND.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_ROOT))
		{
			ROOT.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BASE_GUI))
		{
			GUI.copyFromgrfxML(element);
		}
		else
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.BASE,""+element.getStartLineNo()} );
		}
	}
}
public DataNode getBACKGROUND() {
	return BACKGROUND;
}
public DataBase getClone() {

	DataBase newBase = new DataBase(engine);

	newBase.BACKGROUND = BACKGROUND.getClone();
	newBase.GUI = GUI.getClone();
	newBase.INFO = INFO.getClone();
	newBase.KEY = KEY.getClone();
	newBase.MOUSE = MOUSE.getClone();
	newBase.PROCESS = PROCESS.getClone();
	newBase.PROTOTYPES = PROTOTYPES.getClone();
	newBase.ROOT = ROOT.getClone();
	newBase.DATA = DATA.getClone();

	return newBase;
}
public DataBase getClone(String path) {

	DataBase newBase = new DataBase(engine);

	GrfxMLStoreProcessor.setCloneName( path,this,newBase );

	newBase.BACKGROUND = BACKGROUND.getClone(path);
	newBase.GUI = GUI.getClone(path);
	newBase.INFO = INFO.getClone(path);
	newBase.KEY = KEY.getClone(path);
	newBase.MOUSE = MOUSE.getClone(path);
	newBase.PROCESS = PROCESS.getClone(path);
	newBase.PROTOTYPES = PROTOTYPES.getClone(path);
	newBase.ROOT = ROOT.getClone(path);
	newBase.DATA = DATA.getClone(path);

	return newBase;
}
public DataStore getDATA() {
	return DATA;
}
public DataNode getGUI() {
	return GUI;
}
public DataTitle getINFO() {
	return INFO;
}
public DataKey getKEY() {
	return KEY;
}
public DataMouse getMOUSE() {
	return MOUSE;
}
public DataProcess getPROCESS() {
	return PROCESS;
}
public DataChildren getPROTOTYPES() {
	return PROTOTYPES;
}
public DataNode getROOT() {
	return ROOT;
}
public DataSettings getSETTINGS() {
	return SETTINGS;
}
public void register(DataLinker store) {

	try{
		store.add(DATA,this);
	}
	catch(LockException le)
	{}//Dummy catch
	DATA.register(store);

	try{
		store.add(SETTINGS,this);
	}
	catch(LockException le)
	{}//Dummy catch
	SETTINGS.register(store);

	try{
		store.add(PROCESS,this);
	}
	catch(LockException le)
	{}//Dummy catch
	PROCESS.register(store);

	try{
		store.add(PROTOTYPES,this);
	}
	catch(LockException le)
	{}//Dummy catch
	PROTOTYPES.register(store);

	try{
		store.add(MOUSE,this);
	}
	catch(LockException le)
	{}//Dummy catch
	MOUSE.register(store);

	try{
		store.add(INFO,this);
	}
	catch(LockException le)
	{}//Dummy catch
	INFO.register(store);

	try{
		store.add(KEY,this);
	}
	catch(LockException le)
	{}//Dummy catch
	KEY.register(store);

	try{
		store.add(BACKGROUND,this);
	}
	catch(LockException le)
	{}//Dummy catch
	BACKGROUND.register(store);

	try{
		store.add(ROOT,this);
	}
	catch(LockException le)
	{}//Dummy catch
	ROOT.register(store);

	try{
		store.add(GUI,this);
	}
	catch(LockException le)
	{}//Dummy catch
	GUI.register(store);
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj, boolean recursive) {
	if(DATA==oldObj)
		DATA=(DataStore)newObj;

	if(SETTINGS==oldObj)
		SETTINGS=(DataSettings)newObj;

	if(PROCESS==oldObj)
		PROCESS=(DataProcess)newObj;

	if(PROTOTYPES==oldObj)
		PROTOTYPES=(DataChildren)newObj;

	if(MOUSE==oldObj)
		MOUSE=(DataMouse)newObj;

	if(INFO==oldObj)
		INFO=(DataTitle)newObj;

	if(KEY==oldObj)
		KEY=(DataKey)newObj;

	if(BACKGROUND==oldObj)
		BACKGROUND=(DataNode)newObj;

	if(ROOT==oldObj)
		ROOT=(DataNode)newObj;

	if(GUI==oldObj)
		GUI=(DataNode)newObj;

	if(recursive)
	{
		DATA.replace(oldObj,newObj,recursive);
		SETTINGS.replace(oldObj,newObj,recursive);
		PROCESS.replace(oldObj,newObj,recursive);
		PROTOTYPES.replace(oldObj,newObj,recursive);
		MOUSE.replace(oldObj,newObj,recursive);
		INFO.replace(oldObj,newObj,recursive);
		KEY.replace(oldObj,newObj,recursive);
		BACKGROUND.replace(oldObj,newObj,recursive);
		ROOT.replace(oldObj,newObj,recursive);
		GUI.replace(oldObj,newObj,recursive);
	}

}
public void resetChanged() {
	super.resetChanged();
	SETTINGS.resetChanged();
	PROCESS.resetChanged();
	DATA.resetChanged();
	PROTOTYPES.resetChanged();
	MOUSE.resetChanged();
	INFO.resetChanged();
	KEY.resetChanged();
	BACKGROUND.resetChanged();
	ROOT.resetChanged();
	GUI.resetChanged();
}
protected void setBACKGROUND(DataNode newBACKGROUND) {
	BACKGROUND = newBACKGROUND;
}
protected void setDATA(DataStore newDATA) {
	DATA = newDATA;
}
protected void setGUI(DataNode newGUI) {
	GUI = newGUI;
}
protected void setINFO(DataTitle newINFO) {
	INFO = newINFO;
}
protected void setKEY(DataKey newKEY) {
	KEY = newKEY;
}
protected void setMOUSE(DataMouse newMOUSE) {
	MOUSE = newMOUSE;
}
protected void setPROCESS(DataProcess newPROCESS) {
	PROCESS = newPROCESS;
}
protected void setPROTOTYPES(DataChildren newPROTOTYPES) {
	PROTOTYPES = newPROTOTYPES;
}
protected void setROOT(DataNode newROOT) {
	ROOT = newROOT;
}
protected void setSETTINGS(DataSettings newSETTINGS) {
	SETTINGS = newSETTINGS;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += SETTINGS.togrfxML(Token.BASE_SETTINGS);
	xml += PROCESS.togrfxML(Token.BASE_PROCESS);
	xml += DATA.togrfxML(Token.BASE_DATA);
	xml += PROTOTYPES.togrfxML(Token.BASE_PROTOTYPES);
	xml += MOUSE.togrfxML(Token.BASE_MOUSE);
	xml += INFO.togrfxML(Token.BASE_INFO);
	xml += KEY.togrfxML(Token.BASE_KEY);
	xml += BACKGROUND.togrfxML(Token.BASE_BACKGROUND);
	xml += ROOT.togrfxML(Token.BASE_ROOT);
	xml += GUI.togrfxML(Token.BASE_GUI);
	xml += "</" + name + ">\n";
	return xml;
}
public String togrfxML(String name,TagModifier modifier) {
	return togrfxML(name);
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
public void translate(DataTranslation t) {
	BACKGROUND.translate(t);
	ROOT.translate(t);
	GUI.translate(t);
}
}
